<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" width="50px" height="50px" />
        <span>教务管理系统</span>
      </div>
      <div>
        <span>{{ welcome }}</span>
        <el-button type="info" size="mini" @click="loginOut">退出</el-button>
      </div>
    </el-header>

    <!-- 内容主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i class="el-icon-more"></i>
        </div>
        <el-menu
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          router
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
        >
          <el-submenu
            :index="item.menuId + ''"
            v-for="item in menuList"
            :key="item.menuId"
          >
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.menuName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              @click="saveNavState('/' + subItem.url)"
              :index="'/' + subItem.url"
              v-for="subItem in item.children"
              :key="subItem.menuId"
            >
              <i :class="subItem.icon"></i>
              <span>{{ subItem.menuName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体区域 -->
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>

        <el-footer>Designed by YeJianfeng</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 欢迎信息
      welcome: '欢迎回来!',
      // 菜单数据
      menuList: [],
      // 是否折叠
      isCollapse: false,
      // 被激活的地址
      activePath: ''
    }
  },

  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    this.welcome = this.welcome + window.sessionStorage.getItem('rootname')
  },

  methods: {
    loginOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('/menu/list')
      if (res.code !== 20000) return this.$message.error(res.msg)
      this.menuList = res.data
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;

  div .el-button {
    margin-left: 20px;
  }

  > div.logo {
    margin-left: 10px;
    display: flex;
    align-items: center;
    font-size: 20px;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.toggle-button {
  background-color: #4a5064;
  height: 20px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.el-footer{
  background-color: #333;
  color: white;
  line-height: 60px;
  text-align: center;
}
</style>
